<template>
  <div>
  <van-cell-group inset :border="false" class="patient-card"  @click="handleSelect">
      <van-cell :label="$util.desNumber(cust.idNo)" :border="false">
        <div class="title" slot="title">
          <b>{{cust.custName}}</b>
          <van-tag round class="tag-blue-light m-l-5">{{$util.setName(cust.relationType, init_data.relationType_list)}}</van-tag>
        </div>
        <div class="value">
          <p class="icon"><van-icon name="sort" /></p>
          <p>切换就诊人</p>
        </div>
      </van-cell>
    </van-cell-group>
    <van-popup v-model="popupCard.isShow" position="bottom" round>
        <h3 class="align-center p-a-15">请选择就诊人</h3>
        <van-radio-group v-model="cust.cardId">
          <van-cell-group>
            <van-cell v-for="(item,index) in popupCard.list" :key="index" :label="$util.desNumber(item.idNo)" clickable @click="selectCard(item)">
              <template #right-icon v-if="item.patientId == cust.patientId">
                <van-radio :name="item.cardId" checked-color="#1989fa" />
              </template>
              <div class="title" slot="title">
                <b>{{item.custName}}</b>
                <van-tag round class="tag-blue-light m-l-5">{{$util.setName(item.relationType, init_data.relationType_list)}}</van-tag>
              </div>
            </van-cell>
          </van-cell-group>
        </van-radio-group>
      </van-popup>
    </div>
</template>

<script>
export default {
  data(){
    return{
      cust:{},
      init_data:{},
      popupCard:{
        isShow: false,
        list: []
      }
    }
  },
  async created(){
    await this.getInitData();
    this.cust = this.getCurrentPatient();
    this.getList();
  },
  methods:{
    getCurrentPatient(){
      let patient = this.$store.state.currentPatient;
      let patient2 = localStorage.getItem('currentPatient');
      if(patient.patientId){
        return patient;
      }
      if(patient2){
        return JSON.parse(patient2);
      }
      this.$dialog.alert({
        title: '提示',
        message: '请先去添加就诊人',
      }).then(() => {
        this.$util.gotoPage('/patient/list');
      });
    },
    async getInitData(){
      this.init_data.relationType_list = await this.$util.getDictList('relation_type');
    },
    getList(){
      this.$http('get', '/admin/custApi/getPatientsList', {}, (data,msg) => {
        this.popupCard.list = data;
        this.$store.dispatch('getCurrentPatient');
      })
    },
    handleSelect(){
      // this.$util.gotoPage('/patient/select', {from: 'report'});
      this.popupCard.isShow = true;
    },
    selectCard(item){
      this.cust = item;
      this.$store.commit('setCurrentPatient',item);
      this.$emit('select', item);
      this.popupCard.isShow = false;
    }
  }
}
</script>

<style>

</style>